package com.yanyilu.basing.controller;


import com.yanyilu.basing.common.R;
import com.yanyilu.basing.common.ResultCode;
import com.yanyilu.basing.entity.Activity;
import com.yanyilu.basing.jwt.JwtIgnore;
import com.yanyilu.basing.service.IActivityService;
import com.yanyilu.basing.util.ParseJwtUtil;
import com.yanyilu.basing.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Sxx
 * @since 2020-10-27
 */
@CrossOrigin
@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    IActivityService iActivityService;

    /**
     * 活动列表
     * @return
     */
    @RequestMapping("/admin/select")
    public R activitySelect(){
        return iActivityService.activitySelect();
    }

    /**
     * 更新活动
     * @return
     */
    @RequestMapping("/admin/update")
    public R activityUpdate(@RequestBody Activity activity){
        String role = ParseJwtUtil.getRole();
        if(role.equals("0")){
            return iActivityService.activityUpdate(activity);
        }else {
            return R.ok(ResultCode.NO_ROLE_ERROR);
        }
    }

    /**
     * 获取是否在报名时间内
     * @return
     */
    @JwtIgnore
    @GetMapping("/frontend/state")
    public R getActivity(){
        Activity activity = iActivityService.selectById(1);
        Integer startTime = activity.getSignUpStartTime();
        Integer endTime = activity.getSignUpEndTime();
        int time = TimeUtil.getTime();
        //System.out.println(time >= startTime && time <= endTime);
        return (time >= startTime && time <= endTime) ? R.ok().put("state",1) : R.ok().put("state",0);
    }

    /**
     * 获取是否在报名时间内
     * @return
     */
    @JwtIgnore
    @GetMapping("/timetest/state")
    public R getActivityTime(){
        Activity activity = iActivityService.selectById(1);
        Integer startTime = activity.getStartTime();
        Integer endTime = activity.getEndTime();
        int time = TimeUtil.getTime();
        //System.out.println(time >= startTime && time <= endTime);
        return (time >= startTime && time <= endTime) ? R.ok().put("state",1) : R.ok().put("state",0);
    }

}
